package test;

public class RingTypeLinkedList {
    //单向环形链表
    Node headNode;
    int size;
//---增
    public void add(Object key){
        Node node=new Node(key,headNode);
        Node lastNode=headNode;
        if(headNode==null){
            node.next=node;
            headNode=node;
            return;
        }
        while(true){
            if(lastNode.next==headNode){
                break;
            }
            lastNode=lastNode.next;
        }
        lastNode.next=node;


    }
//---查
    public void show(){
        Node n=headNode;
        while(true){
            System.out.println(n.key);
            n=n.next;
            if(n.next==headNode){
                System.out.println(n.key);
                break;
            }
        }
    }
//---环形链表丢手帕面试题解法·1
    public void delect(int randomNumber1,int randomNumber2){
        Node n=headNode;
        for(int i=1;i<randomNumber1;i++){
            n=n.next;
        }
        Node previousNode=headNode;
        //被删除的数据为被到丢手帕数据
        if(0==randomNumber2){
            while(true){
                if(previousNode.next==n){break;}
                previousNode=previousNode.next;
            }
        }
        for(int i=0;i<randomNumber2;i++){
            previousNode=n;
            n=n.next;
        }
//如果删除的是头节点
        if(n==headNode){
            headNode=n.next;
        }
        //拿到被删除的节点和被删除的前一个节点
        previousNode.next=n.next;
        //headNode=n.next;

    }
    //节点内部类

    private class Node{

        Object key;
        Node next;
        public Node(){}
        public Node(Object key, Node next) {
            this.key = key;
            this.next = next;
        }

    }
}
